package com.woniuxy.qiantai.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.qiantai.entity.User;
import com.woniuxy.qiantai.service.CartitemService;
import com.woniuxy.qiantai.service.OrderService;
import com.woniuxy.qiantai.service.OrderitemService;
import com.woniuxy.qiantai.service.UserService;
import com.woniuxy.qiantai.utils.CookieUtils;
import com.woniuxy.qiantai.utils.JwtUtils;
import com.woniuxy.qiantai.vo.OrderItemVo;
import com.woniuxy.qiantai.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@RestController
@RequestMapping("/orderitem")
public class OrderitemController {
    @Autowired
    private CartitemService cartitemService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderitemService orderitemService;
    @Autowired
    UserService userService;
    @RequestMapping("createOrder")
    public String createOrder(@RequestParam(value = "productIds[]") Integer[] productIds, Integer addressId, HttpServletRequest request) {
        User user = getCurentUser(request);
/*        User user=new User();
        user.setId(1);*/
        orderService.createOrder(productIds,addressId,user.getId());

        return "ok";
    }

    @RequestMapping("selectOrderVo")
    public List<OrderVo> selectOrderVo(HttpServletRequest request){
        User user = getCurentUser(request);
        /*User user=new User();
        user.setId(1);*/
        return orderService.selectOrderVo(user.getId());
    }
    @RequestMapping("pageOrderVolist")
    public Page<OrderVo> pageOrderVolist(HttpServletRequest request, Integer pageSize, Integer currentPage){
        User user = getCurentUser(request);
        /*User user=new User();
        user.setId(1);*/
        return orderService.pageOrderVolist(user.getId(),pageSize,currentPage);
    }

    private User getCurentUser(HttpServletRequest request) {
        //通过请求拿到cookie中的token 再从token中获取用户名 再根据用户名查找出user对象
        String token = CookieUtils.getUserTokenFromCookie(request);
        String name = JwtUtils.getAccount(token);
        User userByUserName = userService.getUserByUserName(name);
        return userByUserName;
    }
}

